
$(function() {
	$("input[id^='mold_s']:visible").autocomplete(Test.mold);
		
	Tip.supplier();

	$("input[id^='material_s']:visible").autocomplete(Test.material);
	
	Tip.emp('采购员');
	
	$("select[id^='emp_t_s']:visible").change(function() {
		var label = $("label[id^='label_emp_t']:visible");
		var a = $(this).val();
		if (a == 'emp') {
			label.html("采购员：");
		} else if (a == 'oper') {
			label.html("录入员：");
		} 
	});
	
	$("select[id^='category_']:visible").selectChain({
		target: $("select[id^='subclass_']:visible"),
		key: "code",
		value: "name",
		url: 'material.php?do=Material.ajax_category',
		data: { ajax: true }
	}).trigger('change');
});

var RecordInCk = {
	
	popup: function (e, p2, id, status) {
		$.cookie('id_', id);

		p2 = query(p2);
		$.cookie('p2', p2);		
		
		var c1 = 'material.php?do=RecordInCk.edit&id=' + id;
		var c2 = 'material.php?do=RecordInCk.view&id=' + id;

		var c3 = 'material.php?do=RecordInCk.in&id=' + id;
		var c5 = 'material.php?do=RecordInCk.cancel&id=' + id;	
		
		var a1 = ['编辑信息', 'url', 'RecordInCk.dialog_edit_', '650x420'];
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '650x420'];
		var a3 = ['采购入库', 'url', 'RecordInCk.dialog_in', '650x420'];
		
		var a5 = ['取消采购', 'url', 'RecordInCk.dialog_cancel', '650x420'];

		// 采购申请
		if (status == 1) {
			var s = c1 + ", " + c2;

			// return this.popup_(e, [a1, a2], s);
		}
		
		// 等待采购
		if (status == 2) {
			var s = c1 + ", " + c2 + ", " + c5;

			// return this.popup_(e, [a1, a2, a5], s);
		}
		
		var b1 = ['编辑数量', 'url', 'RecordInCk.dialog_amount2_', '650x420'];
		var d1 = 'material.php?do=RecordInCk.amount2&id=' + id;

		// 正在采购
		if (status == 3 || status == 4) {
			var s = d1 + ", " + c1 + ", " + c2 + ", " + c3 + ", " + c5;

			return this.popup_(e, [b1, a1, a2, a3, a5], s);
		}

		// 等待入库
		if (status == 3 || status == 4 || status == 8) {
			
			// var s = c3 + ", " + d1 + ", " + c1 + ", " + c2;
			var s = c3 + ", " + c2;

			return this.popup_(e, [a3, a2], s);
		}
		
		var b1 = ['退货', 'url', 'RecordInCk.dialog_back', '650x420'];
		var d1 = 'material.php?do=RecordInCk.back&id=' + id;
		
		var s = c2;
		
		return this.popup_(e, [a2], s);
	},

	popup_: function (e, a, s) {
		var id = $.cookie('id_');
		
		var ss = 'material.php?do=RecordInCk.ss&id=' + id;
		var bb = ['修改状态', 'url', 'RecordInCk.dialog_ss', '400x200'];
		
		// s += ", " + ss;
		// a.push(bb);
		
		return popup(e, a, s);
	},

	/**
	 * 修改状态
	 */
	dialog_ss: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=RecordInCk.audit&item=88', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {

					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.field = form.field.value;
						body.data = form.data.value;
						body.status = form.status.value;

						$(this).dialog("close");

						var url = 'material.php?do=RecordInCk.ssPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=RecordInCk.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.purchase = form.purchase.value;
						body.mold = form.mold.value;
											
						body.name = form.name.value;
						body.standard = form.standard.value;
						
						body.amount2 = form.amount2.value;
						body.unit2 = form.unit2.value;
						body.price2 = form.price2.value;
						
						body.tax = form.tax.value;
						body.currency = form.currency.value;
						body.total = form.total.value;
						
						body.amount = form.amount.value;
						body.unit = form.unit.value;
						
						body.content = form.content.value;

						$(this).dialog("close");

						var url = 'material.php?do=RecordInCk.editPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	dialog_amount2_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=RecordInCk.audit&item=63', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						
						body.amount2 = form.amount2.value;					
						body.content = form.content.value;

						$(this).dialog("close");

						var url = 'material.php?do=RecordInCk.amount2Post';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	in_validate: function () {
		var validator = $("#form_add").validate({
			rules: {
				place: {
					required: true,
					minlength: 2
				}					
			},
			messages: {
				place: {
					required: "请选择或输入仓库信息",
					minlength: $.validator.format("请输入字数不少于{0}个的仓库信息")
				}
			},
			errorPlacement: function(label, element) {
				if (/^place/.test(element[0].name)) {
					label.insertAfter("#place_t");
				} else {
					label.insertAfter(element);
				}
			},
			success: function(label) {
				label.html("&nbsp;").addClass("checked");
			}
		});

		if (!validator.form()) {
			return false;
		}

		return true;
	},

	dialog_in: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=RecordInCk.audit&item=63', function(json) {
			if (json.status == 1) {
				$('#dialog_add').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},
					
					"入库": function() {
						if (!confirm("确认是否入库？")) {
							return false;
						}

						if (!RecordInCk.in_validate()) {
							return false;
						}

						var form = $("#form_add")[0];
								
						var body = { };

						body.id = form.id.value;
						
						body.emp = form.emp.value;
						body.date = form.date.value;
						body.number = form.number.value;
						
						body.purchase = form.purchase.value;
						body.supplier = form.supplier.value;
						
						body.place = form.place.value;						
						body.content = form.content.value;
						
						body.amount = form.amount.value;
						body.amount3 = form.amount3.value;
						
						body.amount4 = form.amount4.value;
						body.amount5 = form.amount5.value;
						
						amount4 = parseFloat(body.amount4);
						amount5 = parseFloat(body.amount5);
						
						if (is_null(body.number)) {
							alert("请输入供应商的送货单号，送货单不能为空！");
							return false;
						}
						
						if (is_null(body.amount4)) {
							alert("请输入本次入库物品的数量，此项不能为空！");
							return false;
						}
						
						if (amount4 > amount5) {
							alert("本次入库物品的数量不能大于未入库的数量，请重新输入！");
							return false;
						}

						$(this).dialog("close");
						
						var url = 'material.php?do=RecordInCk.inPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_add(url, title, xy);
			} else {
				_error();
			}
		});	
	},
	
	dialog_back: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=RecordInCk.audit&item=63', function(json) {
			if (json.status == 1) {
				$('#dialog_add').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},
					
					"退货": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_add")[0];
								
						var body = { };
						body.id = form.id.value;
						
						body.emp = form.emp.value;
						body.date = form.date.value;
						
						body.place = form.place.value;
						body.amount = form.amount.value;
						
						body.content = form.content.value;

						$(this).dialog("close");
						
						var url = 'material.php?do=RecordInCk.backPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_add(url, title, xy);
			} else {
				_error();
			}
		});	
	},

	/**
	 * 采购取消
	 */
	dialog_cancel: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=RecordInCk.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"提交": function() {
						if (!confirm("确认是否取消？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = { };

						body.id = form.id.value;
						body.content = form.content.value;
						body.status = 6;

						$(this).dialog("close");

						$.post('material.php?do=RecordInCk.cancelPost', body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});	
	},

	expunge_: function (form) {
		$.getJSON('material.php?do=RecordInCk.audit&item=52', function(json) {
			if (json.status == 1) {
				expunge(form);
			} else {
				_error();
			}
		});
	},

	purchase: function (id) {
		var url = "material.php?do=Purchase.view&id=" + id;
		$('#dialog_view').dialog('option', 'buttons', { 		
			"关闭": function() { 
				$(this).dialog("close");
			}
		});

		dialog_view(url, '查看采购单', '800x500');
	},
	
	popup_all: function (e, p2) {
		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'material.php?do=RecordInCk.delete' + p2;
		var c2 = 'material.php?do=RecordInCk.batch_in';
		
		var c9 = 'material.php?do=RecordInCk.xls' + p2;	
			
		var a1 = ['删除信息', 'url', 'Common.list_delete'];	
		var a2 = ['采购入库', 'url', 'RecordInCk.dialog_batch_in', '800x450'];
		
		var a9 = ['导出Excel', 'url', 'jswin', '1024x768'];

		var s = c2 + ", " + c9;

		return popup(e, [a2, a9], s);
	},
	
	dialog_batch_in: function (url, title, xy) {
		
		var result = Common.result();
		$.getJSON('material.php?do=RecordInCk.audit&item=63', function(json) {
			if (json.status == 1) {			
				var c = new Array();
				var d = new Array();
				$("input[name^='c_3_']").each(function() {
					if ($(this).attr("checked")) {
						var s = encodeURIComponent(this.name) + "=" + encodeURIComponent($(this).val());
						c.push(s);
						
						var n = $(this).attr("name");
						var a = n.split('_');
						d.push(a[3]);
					}
				});
				
				$("input[name^='c_4_']").each(function() {
					if ($(this).attr("checked")) {
						var s = encodeURIComponent(this.name) + "=" + encodeURIComponent($(this).val());
						c.push(s);
						
						var n = $(this).attr("name");
						var a = n.split('_');
						d.push(a[3]);
					}
				});
				
				$("input[name^='c_8_']").each(function() {
					if ($(this).attr("checked")) {
						var s = encodeURIComponent(this.name) + "=" + encodeURIComponent($(this).val());
						c.push(s);
						
						var n = $(this).attr("name");
						var a = n.split('_');
						d.push(a[3]);
					}
				});
				
				var cc = '';
				for (i = 0; i < d.length; i++) {
					if (cc.length > 0 && cc != d[i]) {
						alert("操作有误，必须选择同一采购订单的采购明细！");
						return false;
					}
					
					cc = d[i];
				}
				
				var body = c.join("&");
				
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},					
					
					"入库": function() {
						if (!confirm("确认是否入库？")) {
							return false;
						}
						
						var cc = RecordInCk._cc();

						var form = $("#form_edit")[0];
						
						var body = {};
						body.cc = cc;
						body.emp = form.emp.value;
						body.date = form.date.value;
						body.number = form.number.value;
						
						body.purchase = form.purchase.value;
						body.supplier = form.supplier.value;
						
						body.place = form.place.value;						
						body.content = form.content.value;
						
						if (is_null(body.number)) {
							alert("请输入供应商的送货单号，送货单不能为空，若没有可以自行以日期格式编写一个即可。");
							return false;
						}
						
						var url = 'material.php?do=RecordInCk.batch_inPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
						
						$(this).dialog("close");

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				var view = '#dialog_edit';
				$.get(url, body, function(data) {
					$(view).html(data);
				});

				var regex = /^(\d+)(x)(\d+)$/;
				if (regex.test(xy)) {
					var cc = xy.split("x");
					var width = parseInt(cc[0]);
					var height = parseInt(cc[1]);
					$(view).dialog('option', 'height', height);
				} else {
					var width = parseInt(xy);
				}

				$(view).dialog('option', 'title', title);
				$(view).dialog('option', 'width', width);
				$(view).dialog('open');
			} else {
				_error();
			}
		});	
	},
	
	_cc: function () {
		var c = new Array();
		$("input[name^='b_']").each(function() {
			if ($(this).attr("checked")) {
				c.push($(this).val());
			}
		});
		
		return c;
	}
};

